package com.ruoyi.shenji.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.shenji.domain.TableData;
import com.ruoyi.shenji.domain.Vo.TableDataVo;
import com.ruoyi.shenji.mapper.TableDataMapper;
import com.ruoyi.shenji.service.TableDataService;
import com.ruoyi.shenji.utils.TableTreeSelect;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class TableDataServiceImpl extends ServiceImpl<TableDataMapper, TableData> implements TableDataService {
    @Autowired
    private TableDataMapper tableDataMapper;

    @Override
    public String getAncestors(TableData tableData, String roleKey){
        return tableDataMapper.getParent(tableData.getParentId(),roleKey).getAncestors()+","+tableData.getParentId().toString();
    }

    @Override
    public String getRoleKey(Long userId) {
        return tableDataMapper.getRoleKey(userId);
    }

    @Override
    public List<TableDataVo> listWithAuth(TableData tableData,String roleKey) {
        LambdaQueryWrapper<TableData>queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(Objects.nonNull(tableData.getName()),TableData::getName,tableData.getName()).isNotNull(TableData::getAncestors);;
        List<TableData> TableDatas = list(queryWrapper);
        List<TableDataVo> tableDatas = TableDatas.stream()
                .filter(tableData1 -> tableData1.getRoleKey().equals(roleKey)) // 先过滤出roleKey相等的TableData对象
                .map(tableData1 -> {
                    TableDataVo tableDataVo = new TableDataVo();
                    BeanUtils.copyProperties(tableData1, tableDataVo);
                    return tableDataVo;
                })
                .collect(Collectors.toList());
        return tableDatas;
    }

    @Override
    public List<TableDataVo> selectList(TableData tableData) {
        LambdaQueryWrapper<TableData>queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(Objects.nonNull(tableData.getName()),TableData::getName,tableData.getName()).isNotNull(TableData::getAncestors);;
         List<TableData> TableDatas = list(queryWrapper);
         List<TableDataVo> tableDatas=TableDatas.stream().map(tableData1->{
             TableDataVo tableDataVo=new TableDataVo();
             BeanUtils.copyProperties(tableData1,tableDataVo);
             return tableDataVo;
         }).collect(Collectors.toList());
        return tableDatas;
    }

    @Override
    public List<TableData> getByParentId(String id) {
        return tableDataMapper.getByParentId(id);
    }

    @Override
    public TableData getByName(String name, String roleKey){
        return tableDataMapper.getByName(name,roleKey);
    }

    @Override
    public List<TableData> selectTableList(TableData tableData,String roleKey)
    {
        return tableDataMapper.selectTableList(tableData,roleKey);
    }

    /**
     * 得到子节点列表
     */
    private List<TableDataVo> getChildList(List<TableDataVo> list, TableDataVo t)
    {
        List<TableDataVo> tlist = new ArrayList<TableDataVo>();
        Iterator<TableDataVo> it = list.iterator();
        while (it.hasNext())
        {
            TableDataVo n = (TableDataVo) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<TableDataVo> list, TableDataVo t)
    {
        return getChildList(list, t).size() > 0;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<TableDataVo> list, TableDataVo t)
    {
        // 得到子节点列表
        List<TableDataVo> childList = getChildList(list, t);
        t.setChildren(childList);
        for (TableDataVo tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 构建前端所需要下拉树结构
     *
     * @param tables 列表
     * @return 下拉树结构列表
     */
    @Override
    public List<TableDataVo> buildTableTree(List<TableDataVo> tables)
    {
        List<TableDataVo> returnList = new ArrayList<TableDataVo>();
        List<Integer> tempList = tables.stream().map(TableDataVo::getId).collect(Collectors.toList());
        for (TableDataVo table : tables)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(table.getParentId()))
            {
                recursionFn(tables, table);
                returnList.add(table);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = tables;
        }
        return returnList;
    }

    /**
     * 构建前端所需要树结构
     *
     * @param tableList 列表
     * @return 树结构列表
     */
    @Override
    public List<TableTreeSelect> buildTableTreeSelect(List<TableDataVo> tableList)
    {
        List<TableDataVo> tableTrees = buildTableTree(tableList);
        return tableTrees.stream().map(TableTreeSelect::new).collect(Collectors.toList());
    }
    @Override
    public List<TableTreeSelect> selectTableTreeList(TableData tableData,String roleKey)
    {
        List<TableData> tableList = SpringUtils.getAopProxy(this).selectTableList(tableData,roleKey);
        List<TableDataVo> tableDatas = tableList.stream().map(tableData1->{
            TableDataVo tableDataVo=new TableDataVo() ;
            BeanUtils.copyProperties(tableData1,tableDataVo);
            return tableDataVo;
        }).collect(Collectors.toList());
        List<TableTreeSelect> newTree = buildTableTreeSelect(tableDatas);
        return newTree;
    }

    @Override
    public LinkedHashMap<String, Object> getNameByAbbrevList(List<TableData> tableKeyList) {
        LinkedHashMap<String ,Object> map = new LinkedHashMap<>();
        tableKeyList.forEach(item->{
            if(!item.getAbbrev().equals("bhqID")){
                map.put(item.getAbbrev(),item.getName());
            }

        });
        return map;
    }
}
